package com.lkqandzzy.controller;

import com.lkqandzzy.entity.Opportunities;
import com.lkqandzzy.entity.PageInfo;
import com.lkqandzzy.service.OpportunitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OpportunitiesController {

    /**
     * 依赖注入
     */
    @Autowired
    private OpportunitiesService opportunitiesService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findOpportunities")
    public String findOpportunitiesService(String contacts,String state,
                             Integer pageIndex, Integer pageSize, Model model) {

        PageInfo<Opportunities> hi = opportunitiesService.findPageInfo(contacts,state,
                pageIndex,pageSize);
        model.addAttribute("hi",hi);
        return "opportunities_list";


    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportOpportunities", method = RequestMethod.POST)
    @ResponseBody
    public List<Opportunities> exportOpportunities(){
        List<Opportunities>OpportunitiesList = opportunitiesService.getAll();
        return OpportunitiesList;
    }

    /**
     * 添加销售信息
     */
    @RequestMapping(value = "/addOpportunities" ,method = RequestMethod.POST)
    @ResponseBody
    public String addOpportunities( @RequestBody Opportunities opportunities) {
        int d = opportunitiesService.addOpportunities(opportunities);
        return "opportunities_list";
    }

    /**
     * 删除销售信息
     */
    @RequestMapping( "/deleteOpportunities")
    @ResponseBody
    public String deleteRecord(Integer sales_id) {
        int d = opportunitiesService.deleteOpportunities(sales_id);
        return "opportunities_list";
    }

    /**
     * 修改销售信息
     */
    @RequestMapping( "/updateOpportunities")
    public String updateRecord( Opportunities opportunities) {
        int ri = opportunitiesService.updateOpportunities(opportunities);
        return "redirect:/findOpportunities";
    }

    @RequestMapping( "/findOpportunitiesById")
    public String findRecord(Integer sales_id, HttpSession session) {

        Opportunities ri= opportunitiesService.findOpportunitiesById(sales_id);
        session.setAttribute("ri",ri);
        return "opportunities_edit";
    }
}
